package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.DateUtils;
import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.module.entity.app.AppLogAction;
import itsm.isperp.module.repository.app.AppLogActionMapper;

import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2014-09-05 17:30
 */
@Service
public class AppLogActionService extends BaseService<AppLogAction> {

	@Autowired
	protected AppLogActionMapper appLogActionMapper;

	@Override
	public AppLogActionMapper getRepository() {
		return this.appLogActionMapper;
	}

	@SuppressWarnings("unchecked")
	public void recoverData(String id) {
		AppLogAction logAction = this.findOne(id);

		HashMap<String, Object> m = new HashMap<String, Object>();

		m = JsonBuilder.buildNonDefaultBinder().fromJson(
				logAction.getDescription(), m.getClass());

		this.getRepository().recoverData(logAction.getModuleName(), m.keySet(),
				m);
		IUser u = ContextHolder.getLoginUser();
		logAction.setActionType(logAction.getActionType() + "["
				+ u.getFullname() + "(" + u.getUsername() + "),"
				+ DateUtils.formatDateTime(new Date()) + "恢复此数据]");

		this.saveOrUpdate(logAction);
	}

}
